package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.seckill.SeckillGoods;
import com.qingcheng.service.seckill.SeckillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<SeckillGoods> findAll() {
        return seckillGoodsMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<SeckillGoods> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<SeckillGoods> seckillGoodss = (Page<SeckillGoods>) seckillGoodsMapper.selectAll();
        return new PageResult<SeckillGoods>(seckillGoodss.getTotal(),seckillGoodss.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<SeckillGoods> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return seckillGoodsMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<SeckillGoods> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<SeckillGoods> seckillGoodss = (Page<SeckillGoods>) seckillGoodsMapper.selectByExample(example);
        return new PageResult<SeckillGoods>(seckillGoodss.getTotal(),seckillGoodss.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public SeckillGoods findById(Long id) {
        return seckillGoodsMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param seckillGoods
     */
    public void add(SeckillGoods seckillGoods) {
        seckillGoodsMapper.insert(seckillGoods);
    }

    /**
     * 修改
     * @param seckillGoods
     */
    public void update(SeckillGoods seckillGoods) {
        seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        seckillGoodsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(SeckillGoods.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 标题
            if(searchMap.get("title")!=null && !"".equals(searchMap.get("title"))){
                criteria.andLike("title","%"+searchMap.get("title")+"%");
            }
            // 商品图片
            if(searchMap.get("smallPic")!=null && !"".equals(searchMap.get("smallPic"))){
                criteria.andLike("smallPic","%"+searchMap.get("smallPic")+"%");
            }
            // 商家ID
            if(searchMap.get("sellerId")!=null && !"".equals(searchMap.get("sellerId"))){
                criteria.andLike("sellerId","%"+searchMap.get("sellerId")+"%");
            }
            // 审核状态，0未审核，1审核通过，2审核不通过
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andLike("status","%"+searchMap.get("status")+"%");
            }
            // 描述
            if(searchMap.get("introduction")!=null && !"".equals(searchMap.get("introduction"))){
                criteria.andLike("introduction","%"+searchMap.get("introduction")+"%");
            }

            // 秒杀商品数
            if(searchMap.get("num")!=null ){
                criteria.andEqualTo("num",searchMap.get("num"));
            }
            // 剩余库存数
            if(searchMap.get("stockCount")!=null ){
                criteria.andEqualTo("stockCount",searchMap.get("stockCount"));
            }

        }
        return example;
    }

}
